<?php

class Menu_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
			
		$this->_table = $this->config->item('database_tables');
	}
	
	// Public methods
	public function get_menu_item($id)
	{
		$this->db->select('*');
		$this->db->where('id', $id);
			
		$query = $this->db->get($this->_table['menu'], 1);
			
		if ($query->num_rows() == 1)
		{
			return $query->row_array();
		}
	}

	public function get_menu($lang_id=null)
	{
		$this->db->select('*');
		if($lang_id!=null)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->order_by('order', 'ASC');
		$this->db->order_by('title', 'ASC');
			
		$query = $this->db->get($this->_table['menu']);
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}

	public function create_menu_item($lang_id=null,$title = null, $description = null, $url = null)
	{
		$data = array
					(
						'title' => ($title) ? $title : $this->input->post('title'),
						'description' => ($description) ? $description : $this->input->post('description'),
						'url' => ($url) ? $url : $this->input->post('url'),
						'parent_id'=>1,
						'type'=> 'menu_item',
						'status' => 1,
						'lang_id' => ($lang_id) ?  $lang_id:2,
						'order' => $this->get_last_order() + 1,
						'external' => (strpos($this->input->post('url'), 'http://') === false) ? '0' : '1'
					);
			
		$this->db->insert($this->_table['menu'], $data);
	}

	public function edit_menu_item($id)
	{
		$data = array
					(
						'title' => $this->input->post('title'),
						'description' => $this->input->post('description'),
						'url' => $this->input->post('url'),
						'parent_id'=>1,
						'type'=>'menu_item',
						'external' => (strpos($this->input->post('url'), 'http://') === false) ? '0' : '1'
					);
		$this->db->where('id', $id);
		$this->db->update($this->_table['menu'], $data);
	}

	public function delete_menu_item($id)
	{
		$this->db->where('id', $id);
		$this->db->delete($this->_table['menu']);
		
		$this->reorganize_navigation();
	}
	
	public function move_item_up($id)
	{
		$previous_item_id = $this->get_previous_item_id($this->get_item_position($id));
		
		$this->db->set('order', '`order`-1', FALSE);
		
		$this->db->where('id', $id);
		$this->db->update($this->_table['menu']);
		
		$this->db->set('order', '`order`+1', FALSE);
		
		$this->db->where('id', $previous_item_id);
		$this->db->update($this->_table['menu']);
	}
	
	public function move_item_down($id)
	{
		$next_item_id = $this->get_next_item_id($this->get_item_position($id));
		
		$this->db->set('order', '`order`+1', FALSE);
		
		$this->db->where('id', $id);
		$this->db->update($this->_table['menu']);
		
		$this->db->set('order', '`order`-1', FALSE);
		
		$this->db->where('id', $next_item_id);
		$this->db->update($this->_table['menu']);
	}
	
	public function change_status_menu_item($id=null, $status =0)
	{
		$data = array
					(
						'status' => $status
					);
		$this->db->where('id', $id);
		$this->db->update($this->_table['menu'], $data);
	}
	// Private or protected methods
	public function reorganize_navigation()
	{
		$this->db->select('id');
		
		$query = $this->db->get($this->_table['menu']);
			
		if ($query->num_rows() > 0)
		{
			$result =  $query->result_array();
			
			$i = 0;

			foreach ($result as $row)
			{
				$this->db->set('order', ++$i);
				$this->db->where('id', $row['id']);
				$this->db->update($this->_table['menu']);
			}
		}
	}
	
	protected function get_item_position($id)
	{
		$this->db->select('order');
		$this->db->where('id', $id);
		
		$query = $this->db->get($this->_table['menu'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['order'];
		}
	}
	
	protected function get_last_order()
	{
		$this->db->select('MAX(menu.order) AS pos');
		
		$query = $this->db->get($this->_table['menu'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['pos'];
		}
	}
	
	protected function get_previous_item_id($order)
	{
		$this->db->select('id');
		$this->db->where('order', $order - 1);
		
		$query = $this->db->get($this->_table['menu'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['id'];
		}
	}
	
	protected function get_next_item_id($lang_id=null,$order)
	{
		$this->db->select('id');
		if($lang_id != null)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->where('order >', $order);
		
		$query = $this->db->get($this->_table['menu'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['id'];
		}
	}
}

/* End of file navigation_model.php */
/* Location: ./application/modules/admin/models/navigation_model.php */